<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #operationBeforeExtend="{ record }">
        <a-link @click="download(record)"><icon-download /> 下载</a-link>
      </template>
    </ma-crud>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import attachmentPod from '@/api/logistics/attachmentPod'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'
  import uploadConfig from '@/config/upload'
    import commonApi from '@/api/common'
import dictHelp from "@/utils/dicthelp.js";
const crudRef = ref()




const options = reactive({
  id: 'attachment_pod',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationColumnWidth: 160,
  formOption: {
    viewType: 'drawer',
    width: 600
  },
  api: attachmentPod.getList,
  add: {
    show: true,
    api: attachmentPod.save,
    auth: ['logistics:attachmentPod:save']
  },
  edit: {
    show: true,
    api: attachmentPod.update,
    auth: ['logistics:attachmentPod:update']
  },
  delete: {
    show: true,
    api: attachmentPod.deletes,
    auth: ['logistics:attachmentPod:delete']
  }
})

const getStoreMode = (mode) => {
    return uploadConfig.storageMode[mode.toString()]
  }

const download = async (record) => {
    let url = ''
    let storeMode = getStoreMode(record.storage_mode)

    if (storeMode !== 'LOCAL') {
      Message.info('处理中...')
      const domain = uploadConfig.storage[storeMode]
      url = domain + record.url
    } else {
      //文件上传在本地则请求后端下载
      Message.info('请求服务器下载文件中...')
      url = 'system/downloadById?id=' + record.id
    }

    const response = await commonApi.download(url, 'get')
    if (response) {
      tool.download(response, (storeMode !== 'LOCAL') ? record.origin_name : '')
      Message.success('请求成功，文件开始下载')  
    } else {
      Message.error('文件下载失败')  
    }
  }

const columns = reactive([
  {
    title: "主键",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入主键"
    }
  },
  {
    title: "文件",
    dataIndex: "origin_name",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
  },
  {
    title: "出库单",
    dataIndex: "send_nos",
    formType: "input",
  },
  {
    title: "更新者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "备注",
    dataIndex: "remark",
    formType: "input",
  },
  {
    title: "运单",
    dataIndex: "shipment_nos",
    formType: "textarea",
    search: true,
    hide: true,
  },
  {
    title: "附件",
    dataIndex: "file",
    formType: "upload",
    type: "file",
    multiple: false,
    hide: true,
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "select",
    dict:dictHelp.user,
    hide: true,
    addDisplay: false,
    editDisplay: false
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    showTime: true
  },
])
</script>
<script> export default { name: 'logistics:attachmentPod' } </script>